package cn.tedu.blackhorse.service.impl;

import cn.tedu.blackhorse.ex.ServiceCode;
import cn.tedu.blackhorse.ex.ServiceException;
import cn.tedu.blackhorse.mapper.SalaryMapper;
import cn.tedu.blackhorse.mapper.TeacherMapper;
import cn.tedu.blackhorse.pojo.dto.SalaryAddNewDTO;
import cn.tedu.blackhorse.pojo.dto.SalaryUpdateDTO;
import cn.tedu.blackhorse.pojo.dto.TeacherUpdateDTO;
import cn.tedu.blackhorse.pojo.entity.Salary;
import cn.tedu.blackhorse.pojo.vo.SalaryListItemVO;
import cn.tedu.blackhorse.pojo.vo.SalaryStandardVO;
import cn.tedu.blackhorse.pojo.vo.TeacherStandardVO;
import cn.tedu.blackhorse.service.ISalaryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class SalaryServiceImpl implements ISalaryService {

    @Autowired
    private SalaryMapper salaryMapper;
    @Autowired
    private TeacherMapper teacherMapper;

    @Override
    public void insertSalary(SalaryAddNewDTO salaryAddNewDTO) {
        SalaryStandardVO standVO = salaryMapper.getByTeacherId(salaryAddNewDTO.getTeacherId());
        if (standVO != null){
            String message = "增添失败,目标教师已有工资";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        Salary salary = new Salary();
        BeanUtils.copyProperties(salaryAddNewDTO,salary);
        int row = salaryMapper.insert(salary);
        if (row != 1){
            String message = "增添失败,系统繁忙请稍后重试";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
    }

    @Override
    public void deleteSalary(Long teacherId) {
        SalaryStandardVO standardVO = salaryMapper.getByTeacherId(teacherId);
        if (standardVO == null){
            String message = "删除失败,数据不存在";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        TeacherStandardVO teacher = teacherMapper.getById(teacherId);
        if (teacher != null){
            String message = "删除失败,所删除的数据对应的老师还存在!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        int row = salaryMapper.deleteByTeacherId(teacherId);
        if (row != 1){
            String message = "删除失败,系统繁忙请稍后重试";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
    }

    @Override
    public void updateById(Long id, SalaryUpdateDTO salaryUpdateDTO) {
        SalaryStandardVO standardVO = salaryMapper.getByTeacherId(salaryUpdateDTO.getTeacherId());
        if (standardVO == null){
            String message = "修改失败,数据不存在";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        Salary salary = new Salary();
        salary.setId(id);
        BeanUtils.copyProperties(salaryUpdateDTO,salary);
        int row = salaryMapper.updateByTeacherId(salary);
        if (row != 1){
            String message = "修改失败,系统繁忙强稍后重试";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }
    }


    @Override
    public List<SalaryListItemVO> list() {
        return salaryMapper.list();
    }

    @Override
    public SalaryStandardVO getByTeacherId(Long teacherId) {
        SalaryStandardVO standVO = salaryMapper.getByTeacherId(teacherId);
        if (standVO == null){
            String message = "查询失败,数据不存在";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        return standVO;
    }
}
